import 'package:flutter/material.dart';
import 'package:flutter_wechat/constants.dart' show Constants, AppColors;


enum ActionItems {
  GROUP_CHAT, ADD_FRIEND, QR_SCAN, PAYMENT, HELP
}


class HomeScreen extends StatefulWidget {
  _HomeScreenState createState() => _HomeScreenState();
}

class NavigationIconView {
  String _title;
  IconData _icon;
  IconData _activeIcon;
  BottomNavigationBarItem item;

  // 可选参数，多一个 title: 形参标志
  NavigationIconView({String title, IconData icon, IconData iconActive}) {
    item = BottomNavigationBarItem(
        icon: Icon(icon), activeIcon: Icon(iconActive), title: Text(title));
  }

//必填参数，后续使用的时候没有形参提示
/*
    NavigationIconView(String this._title,IconData this._icon,IconData this._activeIcon){
    item = BottomNavigationBarItem(
      icon: Icon(this._icon),
      activeIcon: Icon(this._activeIcon),
      title: Text(this._title),
    );
  }*/
}

class _HomeScreenState extends State<HomeScreen> {
  List<NavigationIconView> _navigationView;
  int _currentIndex = 0;
  PageController _pageController;
  List pageViews = [
    Container( color: Colors.red,),
    Container( color: Colors.blue,),
    Container( color: Colors.green,),
    Container( color: Colors.yellow,),
  ];

  @override
  void initState() {
    // 只初始化一次
    super.initState();
    _pageController = PageController(initialPage: _currentIndex);

    _navigationView = [
      //必填参数，后续使用的时候没有形参提示
      /*     NavigationIconView("微信", IconData(
          0xe62b,
          fontFamily: Constants.IconFontFamily,
        ), IconData(
          0xe62f,
          fontFamily: Constants.IconFontFamily,

    )),*/
      NavigationIconView(
        title: "微信",
        icon: IconData(
          0xe62b,
          fontFamily: Constants.IconFontFamily,
        ),
        iconActive: IconData(
          0xe62f,
          fontFamily: Constants.IconFontFamily,
        ),
      ),
      NavigationIconView(
        title: "通讯录",
        icon: IconData(
          0xe655,
          fontFamily: Constants.IconFontFamily,
        ),
        iconActive: IconData(
          0xe6c2,
          fontFamily: Constants.IconFontFamily,
        ),
      ),
      NavigationIconView(
        title: "发现",
        icon: IconData(
          0xe629,
          fontFamily: Constants.IconFontFamily,
        ),
        iconActive: IconData(
          0xe746,
          fontFamily: Constants.IconFontFamily,
        ),
      ),
      NavigationIconView(
        title: "我的",
        icon: IconData(
          0xe63b,
          fontFamily: Constants.IconFontFamily,
        ),
        iconActive: IconData(
          0xe632,
          fontFamily: Constants.IconFontFamily,
        ),
      ),
    ];
  }


  @override
  Widget build(BuildContext context) {
    final BottomNavigationBar bottomBar = BottomNavigationBar(
      // 填充色
      fixedColor: Colors.blue,
      items: _navigationView.map((NavigationIconView view) {
        return view.item;
      }).toList(),
      currentIndex: _currentIndex,
      type: BottomNavigationBarType.fixed,
      onTap: (int index) {
        setState(() {
          _currentIndex = index;
          _pageController.animateToPage(index, duration: Duration(milliseconds: 10), curve: Curves.easeInOut);
        });
      },
    );
    // 这里的类型要一步一步走下去，不然，onselect方法，获取不到Value到类型
    List<PopupMenuItem<ActionItems>> popMenuList = [
      PopupMenuItem(
        child: Row(
          children: <Widget>[
            Icon(Icons.print,color: const Color(AppColors.AppBarPopupMenuColor)),
            Padding(padding: EdgeInsets.all(14)),
            Text("添加",style: TextStyle(color: const Color(AppColors.AppBarPopupMenuColor)),),
          ],
        ),
        value: ActionItems.GROUP_CHAT,
      ),
      PopupMenuItem(
        child: Row(
          children: <Widget>[
            Icon(Icons.settings_overscan,color: const Color(AppColors.AppBarPopupMenuColor)),
            Padding(padding: EdgeInsets.all(14)),
            Text("扫一扫",style: TextStyle(color: const Color(AppColors.AppBarPopupMenuColor)),),
          ],
        ),
        value: ActionItems.ADD_FRIEND,
      ),
    ];
    return Scaffold(
      appBar: AppBar(
        title: Text('微信'),
        backgroundColor: Colors.black,
        actions: <Widget>[
          IconButton(
            icon: Icon(
              Icons.search,
              size: 30.0,
            ),
            onPressed: () {
              print("click Search");
            },
          ),
          PopupMenuButton(
            itemBuilder: (BuildContext context) {
              return popMenuList;
            },
            icon: Icon(Icons.add, size: 30.0),

            onSelected: (ActionItems selected) { print('点击的是$selected'); },
          ),
          //边距，插在哪就是,
          Container(width: 20.0),
        ],
      ),
      bottomNavigationBar: bottomBar,
      body: PageView.builder(
          itemBuilder: (BuildContext context, int index) {
            return pageViews[index];
          },
          itemCount: pageViews.length,
        controller: _pageController,
        onPageChanged: (int index) {
            print("滑动到第$index页");
            setState(() {
              _currentIndex = index;
            });
        },
      ),
    );
  }
}
